package gcu.module.owner.controller;

import gcu.module.build.daomain.Build;
import gcu.module.build.service.BuildService;
import gcu.module.owner.daomain.Owner;
import gcu.module.owner.service.OwnerService;
import gcu.module.property_money.dao.MoneyDao;
import gcu.module.property_money.daomain.PropertyMoney;
import gcu.module.property_money.service.MoneyService;
import gcu.module.rbac.daomain.Role;
import gcu.module.rbac.daomain.User;
import gcu.util.Pager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by haol on 2016/9/23.
 */
@Controller
@RequestMapping("/owner")
public class OwnerController {

      private Logger logger = LoggerFactory.getLogger(OwnerController.class);
      @Autowired
      private OwnerService ownerService;
      @Autowired
      private MoneyService moneyService;
      @Autowired
      private BuildService buildService;

      @RequestMapping("/list")
      public String userList(Owner owner, Pager pager, Model model) {
            List<Owner> owners = ownerService.queryOwnerList(owner, pager);
            model.addAttribute("ownerList", owners);
            model.addAttribute("pager", pager);
            List<Build> build_list = buildService.queryBuild();
            model.addAttribute("build_list",build_list);
            return "/owner/list";
      }

      @RequestMapping("/edit/{id}")
      public String edit(@PathVariable Integer id, Model model) {
            model.addAttribute("id", id);
            return "/owner/edit";
      }

      @RequestMapping("/add")
      public String add(Model model) {
            model.addAttribute("user", new User());
            model.addAttribute("id", null);

            List<Build> build_list = buildService.queryBuild();
            model.addAttribute("build_list",build_list);

            return "/owner/edit";
      }

      @RequestMapping("/del/{id}")
      public String del(@PathVariable int id) {
            ownerService.delete(id);

            return "redirect:/owner/list";
      }


      @RequestMapping("/freshen/{id}")
      public String freshen(@PathVariable int id) {
            PropertyMoney money = moneyService.getMoneyByOwnerId(id);
            if (money == null){
                  ownerService.setMoneyInfo(id,0);
            }else {
                  ownerService.setMoneyInfo(id, money.getIsDeadLine());
            }
            return "redirect:/owner/list";
      }

}
